package uk.co.tarbard.svnreporter;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;

public class DiskWriter implements IWriter, ResourceLoaderAware {

	private String writePath;

	public void write(String filename, String content) throws IOException {

		String writeLocation = writePath + File.separatorChar + filename;
		System.out.println(writeLocation);
		FileUtils.writeStringToFile(new File(writeLocation), content);
	}

/**
 * This is called by spring with the resourceLoader
 * It sets the writePath
 */
	public void setResourceLoader(ResourceLoader resourceLoader) {
		Resource resource = resourceLoader.getResource("classpath*:WEB-INF");
		try {
			setWritePath(resource.getFile().getParent());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void setWritePath(String writePath) {
		this.writePath = writePath;
	}

}
